import { BaseWindow } from "../../../component/window/BaseWindow";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { Language } from "../../../../extend/language/Language";
import { NetDispatcher } from "../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../mvc/ServerCommand";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { BaseItemNormal } from "../common/item/BaseItemNormal";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { ItemData } from "../../resource/info/ItemData";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { BaseItem } from "../common/BaseItem";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { ShopRule } from "../../rules/ShopRule";
import { Cache } from "../../cache/Cache";
import { PageSelecter } from "../common/pageSelect/PageSelecter";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class ExpDrugUseWindow
    
    export  class ExpDrugUseWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._expDrugs = [];
            this.setSize(410, 300);
            this.titleHeight = 25;
            this.title = Language.getString(69900);
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStageHandler);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            return;
        }

        /* internal  */onAddToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onBackPackItemsChange);
            this.updateExpDrugProps();
            return;
        }

        /* internal  */onRemoveFromStageHandler(arg1: Object /* flash.events.Event */): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.onBackPackItemsChange);
            return;
        }

        /* internal  */onBackPackItemsChange(arg1): void
        {
            this.updateExpDrugProps();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1=0;
            var loc4=null;
            super.createChildren();
            ObjCreate.createBg(18, 37, 374, 250, this);
            ObjCreate.createBg(30, 180, 350, 2, this, ImagesConst.SplitLine);
            this.createDrugItem(13400000, 50, 55, "1.1");
            this.createDrugItem(13400001, 155, 55, "1.3");
            this.createDrugItem(13400002, 260, 55, "1.6");
            ObjCreate.createGTextField(Language.getString(69675), 25, 190, 200, 20, this, GlobalStyle.textFormat6);
            this._propVec = new Vector.<BaseItemNormal>();
            loc1 = 0;
            while (loc1 < 8) 
            {
                (loc4 = new BaseItemNormal()).x = 35 + loc1 * 42;
                loc4.y = 216;
                loc4.setSize(36, 36);
                loc4.isDragAble = false;
                loc4.isShowLock = true;
                loc4.doubleClickEnabled = true;
                loc4.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.onItemDoubleClick);
                this.addChild(loc4);
                this._propVec.push(loc4);
                ++loc1;
            }
            var loc2=ObjCreate.createGTextField("", 100, 190, 290, 20, this);
            loc2.autoSize = flash.text.TextFieldAutoSize.RIGHT;
            loc2.addEventListener(flash.events.TextEvent.LINK, this.onLinkClickHandler);
            var loc3="<a href=\'event: mallBuy\'><u>" + Language.getString(69676) + "</u></a>" + "    " + "<a href=\'event: compose\'><u>" + Language.getString(69677) + "</u></a>" + "    " + "<a href=\'event: market\'><u>" + Language.getString(69678) + "</u></a>";
            loc2.htmlText = HTMLUtil.addColor(loc3, "#f0ea3f");
            ObjCreate.createFireTip(HTMLUtil.addColor(Language.getString(69699), "#60e71e"), 25, 255, this, true);
            this._pageSelec = ObjCreate.createPageSelecter(285, 260, this);
            this._pageSelec.addEventListener(flash.events.Event.CHANGE, this.onPageSelecChange);
            return;
        }

        /* internal  */onPageSelecChange(arg1): void
        {
            this.updateShowItems();
            return;
        }

        /* internal  */createDrugItem(arg1: int, arg2: int, arg3: int, arg4: string): void
        {
            var loc1=new GTextFormat(FontUtil.defaultName, 12, GlobalStyle.color2Uint, null, null, null, null, null, flash.text.TextFormatAlign.CENTER);
            var loc2=new ItemData(arg1);
            var loc3;
            (loc3 = ObjCreate.createGTextField("", arg2, arg3, 90, 20, this, loc1)).htmlText = ItemsUtil.getItemName(loc2);
            ObjCreate.createBitmap(ImagesConst.PropBg, arg2 + 11, arg3 + 20, this);
            var loc4;
            (loc4 = new BaseItem()).isDragAble = false;
            loc4.label = "";
            ObjCreate.setObjAttri(loc4, arg2 + 15, arg3 + 25, 60, 60, this);
            loc4.itemData = loc2;
            ObjCreate.createGTextField(Language.getStringByParam(69674, arg4), arg2, arg3 + 90, 90, 20, this, loc1);
            return;
        }

        /* internal  */onLinkClickHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            var loc1=arg1.text;
            switch (loc1) 
            {
                case "mallBuy":
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallShowToTab, ShopRule.ShopMallProp));
                    break;
                }
                case "compose":
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SmeltOpenSmeltByPlanCode, {"code": 550000, "isGuide": false}));
                    break;
                }
                case "market":
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MarketOpenSpecialTag, {"oneId": 9, "twoIndex": 5}));
                    break;
                }
            }
            return;
        }

        public updateExpDrugProps(): void
        {
            var loc2=null;
            this._expDrugs = [];
            var loc1=Cache.instance.pack.backPackCache.getAllItems();
            var loc3=0;
            var loc4=loc1;
            for(loc2 of loc4) 
            {
                if (loc2 == null || loc2 == ItemData.nullItemData) 
                {
                    continue;
                }
                if (ItemsUtil.isRoleExpDrug(loc2)) 
                {
                    this._expDrugs.push(loc2);
                    continue;
                }
                if (!(loc2.itemCode == 13009500 || loc2.itemCode == 13009501 || loc2.itemCode == 13009502 || loc2.itemCode == 13009503 || loc2.itemCode == 13009504 || loc2.itemCode == 13009505)) 
                {
                    continue;
                }
                this._expDrugs.push(loc2);
                continue;
            }
            this._pageSelec.maxPage = Math.ceil(this._expDrugs.length / this._propVec.length);
            this.updateShowItems();
            return;
        }

        /* internal  */updateShowItems(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            loc2 = 0;
            while (loc2 < this._propVec.length) 
            {
                loc1 = this._propVec[loc2];
                loc3 = (this._pageSelec.currentPage - 1) * this._propVec.length + loc2;
                if (this._expDrugs.length > loc3) 
                {
                    loc1.itemData = this._expDrugs[loc3] as ItemData;
                }
                else 
                {
                    loc1.itemData = null;
                }
                ++loc2;
            }
            return;
        }

        /* internal  */onItemDoubleClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as BaseItemNormal;
            if (loc1.itemData) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.BackPack_Use, loc1.itemData));
            }
            return;
        }

        /* internal */ /* var */_propVec: Vector.<BaseItemNormal>;

        /* internal */ /* var */_pageSelec: PageSelecter;

        /* internal */ /* var */_expDrugs: Array<any>;
    }
